<?xml version = '1.0' encoding = 'UTF-8'?>
<HBRRepo><variables><variable name="OQP_Version" type="member" usage="const" id="1" product="Planning"><property name="application">SalesPln</property><property name="dimensionInputMode">type</property><property name="dimensionType">Version</property><property name="prompt_text">LABEL_QTP_SLS_PLAN_PRMPT_VERSION</property><property name="scope">ruleset</property><property name="seeded">true</property><value/></variable><variable name="OQP_Year" type="member" usage="const" id="2" product="Planning"><property name="application">SalesPln</property><property name="dimension">Years</property><property name="dimensionInputMode">name</property><property name="prompt_text">LABEL_QTP_SLS_PLAN_PRMPT_YEAR</property><property name="scope">ruleset</property><property name="seeded">true</property><value/></variable></variables><rulesets/><rules><rule id="1" name="OQP_Set Target" product="Planning"><property name="application">SalesPln</property><property name="plantype">OEP_QTP</property><property name="seeded">true</property><variable_references><variable_reference name="OQP_Version" id="1"><property name="application">SalesPln</property><property name="hidden">true</property><property name="rule_name">OQP_Set Target</property><property name="seq">1</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">"OEP_Working"</property></variable_reference><variable_reference name="OQP_Year" id="2"><property name="application">SalesPln</property><property name="hidden">true</property><property name="rule_name">OQP_Set Target</property><property name="seq">2</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">"No Year"</property></variable_reference></variable_references><script type="groovy">			/*RTPS:{OQP_Version} {OQP_Year}*/
			/*PURPOSE: This rule calculates Target Quota Based on percentage input, pushes down growth% to children of Territory or Resource dimension along with product dimension*/
			String Version=rtps.OQP_Version.member.name;			
			String Year=rtps.OQP_Year.member.name;
			List&lt;String> CustomRelDimensions=[];
			List&lt;String> CustomDimMembers=[];
			List&lt;String> CustomDimensions=[];
            List&lt;String> TargetQuotaMbrs=[]
			boolean isDataModified = false;
            boolean isUnitsModel=false;
			String strRowDim;
			String strColDim;
			String strColDimName;
            String driverDimMember;
            String ScenarioName
			List&lt;String> ColumnDimensions =[];
			List&lt;String> RowDimensions =[];
			List&lt;String> NewColumnDims =[];
            int iCnt;
			String strCrossDim = "";
			try
			{
            /*Evalute if Quota planning model is enabled for units or  Amount */
            	Application app=operation.getApplication();
				Cube qtp=operation.getCube(); 
				Dimension dim=app.getDimension(DimensionType.ACCOUNT,qtp);
                 if (dim.hasMember("OQP_Units",qtp)==true)
                    {
                     		isUnitsModel=true;
                            driverDimMember="OQP_Last Year Units";
                    }
                    else
                    {
                  		  isUnitsModel=false;
                          driverDimMember="OQP_Last Year Revenue";                    
                    }
                    
            /*Identify POV Dimensoins and their members*/
			def grid=operation.grid;
			List&lt;DataGrid.HeaderCell> hcells=grid.getPov()
            hcells.each{
				def dimName = it.getDimName();
				String mbrName=it.getMbrName();
				if (!mbrName.equals(Version)  &amp;&amp; !it.getDimensionType().equals(DimensionType.SIMPLE_CURRENCY)   &amp;&amp; !mbrName.equals(Year))
				{
					CustomRelDimensions.add('''@Relative("'''+mbrName+'''",0)''');
					CustomDimMembers.add('''"'''+mbrName+'''"''')
					strCrossDim= strCrossDim + "\""+ mbrName+"\"->"
					CustomDimensions.add('''"'''+dimName+'''"''');
				}
			
            }			
			StringBuilder cscript = new StringBuilder();
			/*Identify Row Dimensions*/
			List&lt;DataGrid.Row> rows = grid.getRows()
			for (DataGrid.Row row : rows) 
			{
				List&lt;DataGrid.HeaderCell> subRows = row.getHeaders();
				for(DataGrid.HeaderCell cell : subRows) 
				  {
					String memName = cell.getDimName();  
				  if (!RowDimensions.contains(memName))
				   {
						RowDimensions.add(memName)      	 
				   }     	 
				 }        
			}
            /*Identify Column Dimensions*/
			List&lt;List&lt;DataGrid.HeaderCell>> cols = grid.getColumns();
			for (List&lt;DataGrid.HeaderCell> cells : cols) 
			{
			 
				for (DataGrid.HeaderCell cell : cells)  
				  {
				   String memName = cell.getMbrName();
				   String dimName = cell.getDimName();
                   String dimType = cell.getDimensionType();
				   if (!ColumnDimensions.contains(dimName))
					  {
						if (memName.equals("YearTotal") || memName.equals(driverDimMember) || dimType.equals("Scenario")  )
						{
							ColumnDimensions.add(dimName)
                            if (dimType.equals("Scenario"))
                            {
                            	ScenarioName=memName;
                            }
						}
						else
						{
						 if(!NewColumnDims.contains(dimName))	
								{
									NewColumnDims.add(dimName)
									strColDimName=dimName;
								}
						}
				   
					 }   	
				 }
						   
			 }
              /* If user changes Target Quota Instead of Percentage*/
            operation.grid.dataCellIterator({ DataCell cell -> cell.edited }).each {
            	if (it.memberNames.contains("OQP_Target Quota") &amp;&amp; it.memberNames.contains("YearTotal"))
            	{
                	for (String item:it.memberNames)
                    {
                     if ((!item.equals("OQP_Target Quota")) &amp;&amp; (!item.equals("YearTotal"))  &amp;&amp; (!item.equals(Year)) &amp;&amp; (!item.equals(Version))  &amp;&amp; (!item.equals(ScenarioName)))
                     {
                     	TargetQuotaMbrs.add('''"'''+item+'''"''')                     	
                     }
                    }
     				isDataModified=true;
                }
		 	}
             
             /*START : Generate Calculation Script  - Main Fix*/
			cscript.append('''SET AGGMISSG ON;\n''');
			cscript.append('''SET UPDATECALC OFF;\n''');
			cscript.append('''FIX("'''+ Year +'''",''')
			cscript.append ('''"'''+Version+'''"''')			
            cscript.append (''',"'''+ScenarioName+'''"''')			
			if (app.getCurrencyMode()!=CurrencyMode.SINGLE_CURRENCY)
            {
				String sCurrency =  operation.grid.pov.find { it.dimName == 'Currency' }.essbaseMbrName 
                //cscript.append (''',"'''+app.getDefaultCurrency()+'''"''')
                cscript.append (''',"'''+ sCurrency +'''"''')
            }
			cscript.append (''')\n''')
             
                
                
            /*Push Growth percentages accross Territory or Resource dimension and  Product dimension*/
           	
                if (RowDimensions.size()==1)
                {
                  def ls=operation.grid.dataCellIterator{DataCell cell -> cell.edited}.collect([] as Set,{ DataCell cell ->
                    cell.getMemberName(RowDimensions[0])
                })
                if (ls.size()>0)
                {
                isDataModified=true;
                }
                strRowDim=ls[0]
                cscript.append('''	FIX(@ICHILDREN("'''+ls[0]+'''"))\n''' )                
                }	
               
                if (NewColumnDims.size() == 1)
                {
                 def Column=operation.grid.dataCellIterator{DataCell cell -> cell.edited}.collect([] as Set,{ DataCell cell ->
                    cell.getMemberName(NewColumnDims[0])
                })

               
                strColDim="OEP_All " + strColDimName                
                }
                  if (strRowDim!=null)
                {
                
                strCrossDim= strCrossDim + "\""+ strRowDim+"\"->"
                }
                if (strColDim!=null)
                {
                
                strCrossDim= strCrossDim + "\""+strColDim+"\"->"
                } 				
			/*Apply Growth percentage to set Target Quota*/
           	if (TargetQuotaMbrs.size()==0)
           	{
           		strCrossDim= strCrossDim +'''"OQP_Growth%"->"BegBalance"'''
           	}
            cscript.append('''		FIX(@Relative("Yeartotal",0)''')
			if (strColDim!=null)
			{
				cscript.append(''',@Relative("'''+strColDim+'''",0)''')
			}
             if (CustomDimMembers.size()>0)
            {
				cscript.append(''','''+CustomRelDimensions.join(''','''))
            }
          
			cscript.append(''')\n''')
			
            if (TargetQuotaMbrs.size()>0)
            {
            	cscript.append('''				"OQP_Target Quota" \n''')
				cscript.append('''				( \n''')            	
            	cscript.append('''					"OQP_Target Quota" = '''+strCrossDim+'''"OQP_Target Quota"->"YearTotal" *("OQP_Allocated Target Quota"/('''+strCrossDim+'''"OQP_Allocated Target Quota"->"YearTotal"));\n''')
				cscript.append('''					"OQP_Allocated Target Quota" = "OQP_Target Quota"; \n''')
                cscript.append('''				) \n''')
            }
            else
            {
            	cscript.append('''				"OQP_Target Quota" \n''')
				cscript.append('''				( \n''')                	
				cscript.append('''					"OQP_Target Quota" = (1+('''+strCrossDim+''')) * "'''+driverDimMember+'''";\n''')
				cscript.append('''					"OQP_Allocated Target Quota" = "OQP_Target Quota"; \n''')
            	cscript.append('''				) \n''')
            	
            }
			cscript.append('''		ENDFIX; \n''') 			
            cscript.append('''	ENDFIX; \n''')          
            /*Aggregate Allocated Target Quota for all children of Territory or Resource dimension*/
            if (CustomDimMembers.size()>0)
            {
				if (strColDimName!=null)
				{
					CustomDimensions.add('''"'''+strColDimName+'''"''')
				}                
				
                cscript.append('''		FIX("OQP_Allocated Target Quota","OQP_Target Quota","'''+driverDimMember+'''","OQP_Number of Reps","OQP_Number of Accounts",@ICHILDREN("'''+strRowDim+'''"))\n''')
                for (iCnt=0;iCnt&lt;CustomDimensions.size()-1;iCnt++)
                {
                cscript.append('''			FIX(@RELATIVE('''+CustomDimensions[iCnt]+''',0))\n''')              
                }                
                for (iCnt=CustomDimensions.size()-1;iCnt>0;iCnt--)
                {
                cscript.append('''				AGG('''+CustomDimensions[iCnt]+''');\n''')                        
                cscript.append('''			ENDFIX\n''')
                }
                cscript.append('''			AGG('''+CustomDimensions[0]+''');\n''')
                cscript.append('''		ENDFIX\n''')
            }
            else
            {
           	 if (strColDimName!=null)
				{
                	cscript.append('''	FIX(''')
					cscript.append ('''"OQP_Allocated Target Quota","'''+driverDimMember+'''",OQP_Target Quota",''')
					cscript.append ('''@ICHILDREN("'''+strRowDim+'''"))\n''');    
					cscript.append('''		AGG(''')
					cscript.append('''"'''+strColDimName+'''"''')				
					cscript.append (''');\n''')
                	cscript.append('''	ENDFIX; \n''')
            	}
            }           
            /* if (TargetQuotaMbrs.size()>0)
            {*/
              cscript.append('''	FIX(''')
              cscript.append ('''@ICHILDREN("'''+strRowDim+'''")''');
              if(CustomDimMembers.size()>0)
              {
                cscript.append (''','''+CustomDimMembers.join(''','''))
              }
              if (strColDim!=null)
              {
                cscript.append(''','''+'''@ICHILDREN("'''+strColDim+'''")''')
              }
              cscript.append(''')\n''')
              /*Recalulate Growth Percentage*/
              cscript.append('''		"OQP_Growth%"''')
              cscript.append('''		( \n''')
              cscript.append('''			"OQP_Growth%"  = (("OQP_Target Quota"->"YearTotal"-"'''+driverDimMember+'''"->"YearTotal")/ "'''+driverDimMember+'''"->"YearTotal");\n''')
              cscript.append('''		) \n''')
              cscript.append('''	ENDFIX; \n''')				
            /*}*/
            
			cscript.append('''ENDFIX; \n''')
			if (isDataModified==true) 
			{
			println (cscript.toString())
			return cscript.toString()
			}
			else
			{
			println ("No cells modified") 
			}
			}catch(all) {
				throw new HspRuntimeException('Error occured while executing business rule');
			}							
			</script></rule></rules><components/><deployobjects><deployobject product="2" application="salespln" plantype="oep_qtp" obj_id="1" obj_type="1" name="OQP_SET TARGET"/></deployobjects></HBRRepo>